<?php

/**
 * Terminal Test
 * php .\omnicore.php getTransactionByHash e0e3749f4855c341b5139cdcbb4c6b492fcc09c49021b8b15462872b4ba69d1b
 * php .\omnicore.php getTransactionByAddress 1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P 0
 */
$arg1 = $argv[1];
$arg2 = $argv[2];
$arg3 = $argv[3];

switch ($arg1) {
    case "getTransactionByHash":
        $transaction = getTransactionByHash($arg2);
        var_dump($transaction);
    break;
    case "getTransactionByAddress":
        $transaction = getTransactionByAddress($arg2, $arg3);
        var_dump($transaction);
    break;
    default:
        var_dump("nothing to do");
}

function getTransactionByHash($hash) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.omniexplorer.info/v1/transaction/tx/" . $hash,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "Cookie: __cfduid=d8c2f0d52918dda848e3477e0bb8efc801587801023"
        ),        
        CURLOPT_SSL_VERIFYPEER =>  FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
    ));

    $response = curl_exec($curl);
    if ($response === false) echo "curl error: " . curl_error($curl) . "\n";
    curl_close($curl);

    return $response;
}

function getTransactionByAddress($address, $page) {
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.omniexplorer.info/v1/transaction/address",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "addr=" . $address . "&page=" . $page,
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
        "Cookie: __cfduid=da5396aaeb3f1263de829ae9f73b92c5d1587803635"
      ),
      CURLOPT_SSL_VERIFYPEER =>  FALSE,
      CURLOPT_SSL_VERIFYHOST => FALSE,
    ));
    
    $response = curl_exec($curl);
    if ($response === false) echo "curl error: " . curl_error($curl) . "\n";
    curl_close($curl);

    return $response;
}




function bitcoinApiList() {
    require_once('./lib/easybitcoin.php');
    $bitcoin = new Bitcoin('user','pass','192.168.1.217','8332');

    $blocknumber = $bitcoin->getblockcount();
    var_dump($blocknumber);

    $transaction = $bitcoin->getrawtransaction('0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098', 1);
    var_dump($transaction);

    $block = $bitcoin->getblock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
    var_dump($block);

    $balance = $bitcoin->omni_getbalance("1Cau6yFGqLGwLMWL7say7rA8eHUipnCmG1", 31);
    var_dump($balance);
}
